package org.hpin.asset.web;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hpin.asset.entity.Suppliers;
import org.hpin.asset.service.SuppliersService;
import org.hpin.common.core.SpringTool;
import org.hpin.common.core.web.BaseAction;
import org.hpin.common.util.HttpTool;
import org.hpin.common.util.StrUtils;
import org.hpin.common.widget.pagination.Page;

@Namespace("/asset")
@Action("suppliersAction")
@Results({
	@Result(name = "listSuppliers" ,location = "/WEB-INF/suppliers/listSuppliers.jsp"),
	@Result(name = "addSuppliers", location = "/WEB-INF/suppliers/addSuppliers.jsp"),
	@Result(name = "browSuppliersAll", location = "/WEB-INF/suppliers/browSuppliersAll.jsp"),
	@Result(name = "modifySuppliersAll", location = "/WEB-INF/suppliers/modifySuppliersAll.jsp"),
	@Result(name = "modifySuppliers", location = "/WEB-INF/suppliers/modifySuppliers.jsp"),
	@Result(name = "browSuppliers", location = "/WEB-INF/suppliers/browSuppliers.jsp"),
	@Result(name = "lookUpSuppliers", location = "/WEB-INF/suppliers/lookUpSuppliers.jsp"),
	})
public class SuppliersAction extends BaseAction{
	
	private static final long serialVersionUID = 345061742245126484L;
	SuppliersService service=(SuppliersService) SpringTool.getBean(SuppliersService.class);
	private Suppliers suppliers;
	
	public Suppliers getSuppliers() {
		return suppliers;
	}
	public void setSuppliers(Suppliers suppliers) {
		this.suppliers = suppliers;
	}
	/**
	 * 查询所有供应商
	 * @return
	 * @throws ParseException
	 */
	public String listSuppliers() throws ParseException{
		page = new Page(HttpTool.getPageNum() , HttpTool.getPageSize());
		Map paramsMap = buildSearch();
		String orderField  = HttpTool.getParameter("orderField");
		String orderDirection = HttpTool.getParameter("orderDirection");
		if( paramsMap.containsKey("orderDirection")){
			paramsMap.remove("orderDirection");
		}
		if( paramsMap.containsKey("orderField")){
			paramsMap.remove("orderField");
		}
		if (StrUtils.isNotNullOrBlank(orderField)&&StrUtils.isNotNullOrBlank(orderDirection)) {
			paramsMap.put(orderField,orderDirection);
		}
		HttpTool.setAttribute("orderField", orderField);
		HttpTool.setAttribute("orderDirection", orderDirection);
		findByPage(page, paramsMap);
		return "listSuppliers";
	}
	public Page findByPage(Page page , Map paramsMap){
		paramsMap.put("filter_and_isDeleted_EQ_I",0);
		service.findByPage(page, paramsMap);
		return page ;
	}
	/**
	 * 删除供应商
	 * @return
	 */
	public String deleteSuppliers(){
		JSONObject json = new JSONObject();
		try {
			String[] id = ids.replaceAll(" ", "").split(",");
			service.delete(id);
			json.accumulate("statusCode", 200);
			json.accumulate("message", "操作成功");
			json.accumulate("navTabId", super.navTabId);
			json.accumulate("callbackType", "");
			json.accumulate("forwardUrl", "");
			json.accumulate("confirmMsg", "");
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		renderJson(json);
		return null;
		
	}
	/**
	 * 查看供应商详情
	 * @return
	 */
	public String browSuppliers(){
		String flag = HttpTool.getParameter("flag");
		suppliers=(Suppliers) service.findById(id);
		HttpTool.setAttribute("flag", flag);
		return "browSuppliers";
	}
	/**
	 * 修改供应商信息前查询
	 */
	public String modifySuppliersAll(){
		HttpTool.setAttribute("SuppliersId", id);
		return "modifySuppliersAll";
	}
	
	/**
	 * 修改供应商页面
	 */
	public String modifySuppliers(){
		suppliers = (Suppliers)service.findById(id);
		return "modifySuppliers";
	}
	public String addSuppliers(){
		if(StrUtils.isNotNullOrBlank(id)){
			suppliers = (Suppliers)service.findById(id);
			
		}else{
			suppliers = new Suppliers();
		}
		return "addSuppliers";
	}
	/** 添加供应商基本信息*/
	public String saveSuppliers(){
		JSONObject json = new JSONObject();
		try {
			String currentTabId = HttpTool.getParameter("currentTabId");
			if(StrUtils.isNotNullOrBlank(suppliers.getId())){
				if(null!=suppliers){
					System.out.println(suppliers.getId());
					service.update(suppliers);
					json.accumulate("statusCode", 200);
					json.accumulate("message", "操作成功");
					json.accumulate("navTabId", super.navTabId);
					json.accumulate("currentTabId", currentTabId);
					json.accumulate("callbackType", "closeCurrent");
				}
			}else{
				suppliers.setId(null);
				suppliers.setIsDeleted(0);
				service.save(suppliers);
				System.out.println(suppliers.getIsDelete());
				json.accumulate("statusCode", 200);
				json.accumulate("message", "操作成功");
				json.accumulate("navTabId", super.navTabId);
				json.accumulate("currentTabId", currentTabId);
				json.accumulate("callbackType", "closeCurrent");
				}
		} catch (Exception e) {
			json.accumulate("statusCode", 300);
			json.accumulate("message", "操作失败");
			e.printStackTrace();
		}
		//json.accumulate("callbackType", suppliers.getId());
		renderJson(json);
		return null;
	}
	public String browSuppliersAll() throws ParseException{
		String flag = HttpTool.getParameter("flag");
		HttpTool.setAttribute("id", id);
		HttpTool.setAttribute("flag", flag);
		return "browSuppliersAll";
	}
	/**
	 * 供应商列表
	 */
	public String lookUpSuppliers() throws ParseException {
		page = new Page(HttpTool.getPageNum(), HttpTool.getPageSize());
		Map paramsMap = buildSearch();
		paramsMap.put("filter_and_isDeleted_EQ_I", 0);
		List<Suppliers> suppliersList = service.findByPage(page, paramsMap);
		page.setResults(suppliersList);
		return "lookUpSuppliers";
	}

}
